const path = require("path");
const { VueLoaderPlugin } = require("vue-loader"); // 正确导入方式
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { type } = require("os");
module.exports = {
  entry: "./src/main.js",

  output: {
    filename: "chat-widget.js",
    path: path.resolve(__dirname, "dist"),
    library: "AIChatWidget",
    libraryTarget: "umd",
    umdNamedDefine: true,
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
    extensions: [".js", ".vue", ".json", ".css", ".png", ".svg"],
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: "vue-loader",
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        type: "asset/resource",
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: "index.html",
    }),
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, "dist"), // 旧的 contentBase 需要改成 static
    },
    compress: true, // 开启 gzip 压缩
    port: 9040, // 本地开发端口
    hot: true, // 启用热更新
    open: true, // 自动打开浏览器
  },
};
